package com.example.demo.exception;

import com.example.demo.utils.HttpCode;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.slf4j.helpers.MessageFormatter;

/**
 * 用于前端提示信息异常
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class BusinessException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    public HttpCode httpCode;

    private String detailMessage;

    private Integer code;



    public BusinessException(HttpCode httpCode) {
        super(httpCode.getMsg());
        this.httpCode = httpCode;
        this.detailMessage = httpCode.getMsg();
        this.code = httpCode.getCode();
    }

    public BusinessException(HttpCode httpCode, String msg) {
        super(msg);
        this.httpCode = httpCode;
        this.detailMessage = msg;
        this.code = httpCode.getCode();
    }

    public BusinessException(Throwable e) {
        super(e);
        this.detailMessage = e.getMessage();
        if(e instanceof BusinessException){
            BusinessException businessException = (BusinessException)e;
            this.httpCode = businessException.httpCode;
            this.code = businessException.getCode();
        }else {
            this.httpCode = HttpCode.INTERNAL_SERVER_ERROR;
            this.code = HttpCode.INTERNAL_SERVER_ERROR.getCode();
        }
    }
    public BusinessException(String message) {
        super(message);
        this.detailMessage = message;
        this.httpCode = HttpCode.INTERNAL_SERVER_ERROR;
        this.code = HttpCode.INTERNAL_SERVER_ERROR.getCode();
    }

    public BusinessException(HttpCode httpCode, String format, Object ...argArray) {
        super(MessageFormatter.arrayFormat(format, argArray).getMessage(), MessageFormatter.format(format, argArray).getThrowable());
        this.detailMessage = MessageFormatter.arrayFormat(format, argArray).getMessage();
        this.httpCode = httpCode;
        this.code = httpCode.getCode();
    }

    public BusinessException(String format, Object ...argArray) {
        super(MessageFormatter.arrayFormat(format, argArray).getMessage(), MessageFormatter.format(format, argArray).getThrowable());
        this.detailMessage = MessageFormatter.arrayFormat(format, argArray).getMessage();
        this.httpCode = HttpCode.INTERNAL_SERVER_ERROR;
        this.code = HttpCode.INTERNAL_SERVER_ERROR.getCode();
    }

    public BusinessException(String message, Throwable e) {
        super(message, e);
        this.detailMessage = message;
        if(e instanceof BusinessException){
            BusinessException businessException = (BusinessException)e;
            this.httpCode = businessException.httpCode;
            this.code = businessException.getCode();
        }else {
            this.httpCode = HttpCode.INTERNAL_SERVER_ERROR;
            this.code = HttpCode.INTERNAL_SERVER_ERROR.getCode();
        }
    }

    public BusinessException(Integer code, String message, Throwable e) {
        super(message, e);
        this.code = code;
        this.detailMessage = message;
        if(e instanceof BusinessException){
            BusinessException businessException = (BusinessException)e;
            this.httpCode = businessException.httpCode;
        }else {
            this.httpCode = HttpCode.INTERNAL_SERVER_ERROR;
        }
    }

    @Override
    public String getMessage(){
        return detailMessage;
    }

}

